/**   
 * Copyright 2011 COMERE Harold
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package etrl.shaders;

/**
 * \class CustomVariable 
 * \brief Specialized Variable describing a variable nor
 * uniform nor vertex attribute.
 * 
 * @author harold
 * 
 */
class CustomVariable extends Variable
{
	/**
	 * \brief Initializes the custom variable
	 * @param name Name of the variable
	 * @param type Type of the variable
	 * @param forwardMode Forward mode to apply on the variable
	 * @param elementCount Element count, if > 1, the variable is an array
	 */
	public CustomVariable(String name, GLSLType type, VariableForwardMode forwardMode, int elementCount)
	{
		super(name, type, forwardMode, true, false, elementCount);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getDeclarationInVertexShader()
	{
		String declaration = new String("");

		String arraySizeString = new String("" + _arraySize);

		switch (_forwardMode)
		{
		case VERTEX_ONLY:
			declaration += _type.asString() + " " + _name;
			if (_array)
				declaration += "[" + arraySizeString + "]";
			declaration += ";\n";
			break;

		case VERTEX_AND_FRAGMENT:
			declaration += "varying " + _type.asString() + " out_" + _name;
			if (_array)
				declaration += "[" + arraySizeString + "]";
			declaration += ";\n";
			break;

		case FRAGMENT_ONLY:
			// No declaration in vertex shader
			break;
		}

		return declaration;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getDeclarationInFragmentShader()
	{
		String declaration = new String("");
		String arraySizeString = new String("" + _arraySize);

		switch (_forwardMode)
		{
		case VERTEX_ONLY:
			// No declaration in fragment shader
			break;

		case VERTEX_AND_FRAGMENT:
			declaration += "varying " + _type.asString() + " out_" + _name;
			if (_array)
				declaration += "[" + arraySizeString + "]";
			declaration += ";\n";
			break;

		case FRAGMENT_ONLY:
			declaration += _type.asString() + " " + _name;
			if (_array)
				declaration += "[" + arraySizeString + "]";
			declaration += ";\n";
			break;
		}

		return declaration;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getInitialization()
	{
		return "";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getBindingId()
	{
		return "";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String nameWithPrefix(ShaderStage stage)
	{
		String nameWithPrefix = new String("");

		switch (stage)
		{
		case VERTEX:

			switch (_forwardMode)
			{
			case VERTEX_ONLY:
				nameWithPrefix = _name;
				break;

			case VERTEX_AND_FRAGMENT:
				nameWithPrefix = "out_" + _name;
				break;

			default:
				System.out.println("[CustomVariable::nameWithPrefix] Unexpected error : How do you get there ??");
				break;
			}

			break;

		case FRAGMENT:

			switch (_forwardMode)
			{
			case FRAGMENT_ONLY:
				nameWithPrefix = _name;
				break;

			case VERTEX_AND_FRAGMENT:
				nameWithPrefix = "out_" + _name;
				break;

			default:
				System.out.println("[CustomVariable::nameWithPrefix] Unexpected error : How do you get there ??");
				break;
			}

			break;
		}

		return nameWithPrefix;
	}

}
